<template>
	<view>
		<view class="category-list">
			<!-- 左侧分类导航 -->
			<scroll-view scroll-y="true" class="left">

				<view class="row" v-for="(category, index) in categoryList" :key="category.cid"
					:class="[index == showCategoryIndex ? 'on' : '']" @click="showCategory(index)">
					<view class="text">
						<view class="block"></view>
						{{ category.name }}
					</view>
				</view>
			</scroll-view>
			<!-- 右侧子导航 -->
			<scroll-view scroll-y="true" class="right">
				<view class="category">
					<view class="list">
						<view class="box" v-for="(box, i) in categoryChildren" :key="i">
							<image @click="toCategory(box)" :src="box.cover"
								lazy-load="https://taobao.xianmxkj.com/logo.png"></image>
							<view class="multi3 text-box">
								<view class="text multi2">{{ box.name }}</view>
								<view class="price">
									<text class="font-26">￥</text>
									<text class="font-30 pad-left-right-10 max">{{box.min_price}}</text>
									<text class="font-26">元起</text>
									<!-- <text>/{{info.unit}}</text> -->
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	//高德SDK
	export default {
		props: {
			categoryList: {
				type: Array,
				default () {
					return []
				}
			},
			selected: {
				type: Number,
				default () {
					return 0
				}
			}
		},
		data() {
			return {
				headerPosition: 'fixed',
				categoryChildren: []
			};
		},
		computed: {
			showCategoryIndex: {
				get() {
					return this.selected;
				},
				set(newValue) {
					console.log(newValue)
				}
			}

		},
		watch: {
			categoryList: {
				handler: function(newData, oldData) {
					this.categoryChildren = newData[0]?.children;
				},
				immediate: true // 在组件初始化时立即触发监听
			},
		},

		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			if (e.scrollTop >= 0) {
				this.headerPosition = 'fixed';
			} else {
				this.headerPosition = 'absolute';
			}
		},
		methods: {
			//分类切换显示
			showCategory(index) {
				this.$emit("selectedClick", index);
				// this.showCategoryIndex = index;
				this.categoryChildren = this.categoryList[index]?.children;
			},
			toCategory(item) {

				this.$emit("itemClick", item);
			}
		}
	};
</script>
<style lang="scss">
	.category-list {
		width: 100%;
		background-color: #fff;
		display: flex;


		.left,
		.right {
			// 修复不能置顶问题
			top: 0px;

			bottom: 0upx;
		}

		.left {
			position: fixed;
			width: 24%;
			left: 0upx;
			background-color: #f2f2f2;
			/* #ifdef H5 */
			padding-top: 88rpx;
			/* #endif */

			.row {
				width: 100%;
				height: 90upx;
				display: flex;
				align-items: center;

				.text {
					width: 100%;
					position: relative;
					font-size: 28upx;
					display: flex;
					justify-content: center;
					color: #3c3c3c;

					.block {
						position: absolute;
						width: 0upx;
						left: 0;
					}
				}

				&.on {
					height: 100upx;
					background-color: #fff;

					.text {
						font-size: 30upx;
						font-weight: 600;
						color: #2d2d2d;

						.block {
							width: 10upx;
							height: 80%;
							top: 10%;
							background-color: #0099DE;
						}
					}
				}
			}
		}

		.right {
			margin-left: 24%;
			width: 76%;
			left: 24%;

			.category {
				width: 94%;
				padding: 20upx 3%;

				.banner {
					width: 100%;
					height: 24.262vw;
					border-radius: 10upx;
					overflow: hidden;
					box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.3);

					image {
						width: 100%;
						height: 24.262vw;
					}
				}

				.list {
					margin-top: 40upx;
					width: 100%;
					display: flex;
					flex-wrap: wrap;

					.box {
						width: calc(71.44vw / 2);
						margin-bottom: 40upx;
						display: flex;
						// justify-content: center;
						align-items: center;
						flex-wrap: wrap;
						// position: relative;
						padding: 0 20rpx;
						box-sizing: border-box;

						image {
							width: 100%;
							height: calc(71.44vw / 2 - 20rpx);
						}
						
						.text-box{
							height: 134rpx;
						}

						.price {
							// position: absolute;
							// right: 20rpx;
							// top: 0rpx;
							color: #FF4913;
							// padding: 10rpx 20rpx;
							// border-radius: 0 0 0 20rpx;
							// background: rgba(0, 0, 0, .3);
							margin-top: 10rpx;
						}

						.text {
							margin-top: 5rpx;
							width: 100%;
							// display: flex;
							// justify-content: center;
							font-size: 26upx;
							// height: 74rpx;
						}
					}
				}
			}
		}
	}
</style>